package jwlee.testthread;

public class MyThreadLocal {
	private int seqNum;

	private ThreadLocal<Integer> seqNumTL = new ThreadLocal<Integer>() {
		public Integer initialValue() {
			return 0;
		}
	};

	public int getNextNum() {
		return ++seqNum;
	}

	public int getNextNumTL() {
		seqNumTL.set(seqNumTL.get() + 1);
		return seqNumTL.get();
	}

	private static class TestThread extends Thread {
		private MyThreadLocal sn;

		public TestThread(MyThreadLocal sn) {
			this.sn = sn;
		}

		public void run() {
			for (int i = 0; i < 3; i++) {
				System.out.println(Thread.currentThread().getName()
						+ " NextNum:" + sn.getNextNum() + " NextNumTL:"
						+ sn.getNextNumTL());
			}
		}
	}

	public static void main(String[] args) {
		MyThreadLocal sn = new MyThreadLocal();

		TestThread t1 = new TestThread(sn);
		TestThread t2 = new TestThread(sn);
		TestThread t3 = new TestThread(sn);
		t1.start();
		t2.start();
		t3.start();
	}

}
